package net.sparrow.module;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.pbuffer.FBOGraphics;

import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.EnumSkyBlock;
import net.minecraft.src.ScaledResolution;
import net.sparrow.Sparrow;
import net.sparrow.event.DataField;
import net.sparrow.event.SpEventListener;
import net.sparrow.event.SpGuiRenderEvent;
import net.sparrow.event.SpTickEvent;
import net.sparrow.gui.drawing.SpColor;
import net.sparrow.gui.drawing.SpScreenCoord;
import net.sparrow.util.Util;
import net.sparrow.cmd.CommandHandler;

public class AutoFullbrightModule extends Module {

	@DataField( id = "world.autofullbright")
	public Boolean autoFullbright = true;
	
	//TODO based off block you're looking at?
	//TODO changeable threshold
	@SpEventListener
	public void onTick(SpTickEvent e) {

		EntityClientPlayerMP player = Sparrow.mc().player();
		double light = Sparrow.mc().world().getSavedLightValue(EnumSkyBlock.Sky, 
				(int)player.posX, (int)player.posY, (int)player.posZ) - (Sparrow.mc().world().isDaytime()?0:4);
		//Util.print(light + "" + Sparrow.mc().world().isDaytime());
		if(!autoFullbright) {
			Sparrow.mc().minecraft().gameSettings.gammaSetting = 1f;
			return;
		}
		float brightness = Sparrow.mc().minecraft().gameSettings.gammaSetting;
		double goalLight = 16 - light; //goal gammaBrightness
		if(brightness < goalLight) {
			Sparrow.mc().minecraft().gameSettings.gammaSetting += .1;
		}
		if(brightness > goalLight) {
			Sparrow.mc().minecraft().gameSettings.gammaSetting -= .1;
		}
		if(Math.abs(brightness - goalLight) < .25) {
			Sparrow.mc().minecraft().gameSettings.gammaSetting = (float) goalLight;
		}	
	}
	
	double xPos = 5;
	double yPos = 5;
	@SpEventListener
	public void onGuiRender(SpGuiRenderEvent e) {
		ScaledResolution sr = new ScaledResolution(Sparrow.mc().minecraft().gameSettings, Sparrow.mc().minecraft().displayWidth, Sparrow.mc().minecraft().displayHeight);
        int sw = sr.getScaledWidth();
        int sh = sr.getScaledHeight();
		if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			xPos--;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			xPos++;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			yPos++;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			yPos--;
		}
		GL11.glPushMatrix();
		//this.disableDefaults();
		//this.setupOverlayRendering();
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glDisable(GL11.GL_LIGHTING); //2896. Lighting does not apply to this "layer"
		GL11.glEnable(GL11.GL_ALPHA_TEST);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // transparent color enabled basically
		GL11.glDisable(GL11.GL_TEXTURE_2D); //3553. Not drawing textures, just colors
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glColor4f(.1f, .1f, .1f, .3f);
		//GL11.glVertex2d(25, 25);
		//GL11.glVertex2d(25, 50);
		GL11.glVertex2d(xPos * .01 * sw, yPos * .01 * sh);
		GL11.glVertex2d(xPos * .01 * sw ,(yPos + 1) * .01 * sh);
		GL11.glColor4f(.1f, .1f, .1f, 0f);
		//GL11.glVertex2d(50, 50);
		//GL11.glVertex2d(50, 25);
		GL11.glVertex2d((xPos + 1) * .01 * sw, (yPos + 1) * .01 * sh);
		GL11.glVertex2d((xPos + 1) * .01 * sw, yPos * .01 *sh);
		GL11.glEnd();
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glPopMatrix();
	}
	
	public CommandHandler loadCommands() {
		return new CommandHandler().withDefaultToggle("world.autofullbright");
	}
}
